/*
 * Copyright (C) 2007 Evan Long    longx223 (at) umn (dot) edu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/**
 * @file   monitor_loop.c
 * @author Evan Long
 * 
 * @brief  Main loop of the program. Reads in images, looks for gestures, and
 * executes commands until the user terminates the program.
 * 
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vlc/vlc.h>
#include "monitor_loop.h"
#include "gesture_recognition.h"
#include "framegrabber.h"
#include "image_proc.h"
#include "cvector.h"
#include "util.h"
#include "vlc_control.h"

void monitor_loop(int argc, char** argv)
{
  monitor_state_t state = MONITOR;
  frame_grabber_t* frame = new_frame_grabber("/dev/video");
  cvector_t* points;

  vlc_control_t* vlc = new_vlc_control(argc, argv);
  
  while(1)
  {
    point_t p = find_brightest_point(grab_frame(frame), frame->width, 
				     frame->height);
    if(p.x == -1)
    {
      if(state == RECORD)
      {
	state = MONITOR;
	char* cmd = detect_gesture(points);
	printf("%s\n", cmd);

	exec_vlc_command(vlc, cmd);
	
	free(cmd);
	free_vector(points);
      }
    }
    else
    {
      if(state == MONITOR)
      {
	state = RECORD;
	points = new_cvector(sizeof(point_t));
      }
      add_element(points, &p);
    }
  }
}
